package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateSaveSipClusterDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateSipClusterPageDTO;
import com.itlong.cloud.POJO.PO.SipClusterPO;
import com.itlong.cloud.POJO.VO.operate.OperateSipClusterVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.operate.dao.IOperateSipClusterDao;
import com.itlong.cloud.operate.service.IOperateSipClusterService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * <desc>
 *      运营-运营管理-SIP集群管理服务接口实现类。
 * <desc/>
 *
 * @createDate 2017/9/30.
 */
@Service
public class IOperateSipClusterServiceImpl implements IOperateSipClusterService {

    private static final Logger LOG = LoggerFactory.getLogger(IOperateSipClusterServiceImpl.class);

    @Autowired
    IOperateSipClusterDao iOperateSipClusterDao;

     /**
      * <desc>
      *      保存SIP服务器信息。
      * <desc/>
      *
      * @param  saveDTO SIP数据传输对象
      * @return  返回影响行数
      * @author shengen.T
      * @createDate 2017/9/30
      */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer saveSipInfo(OperateSaveSipClusterDTO saveDTO)  throws Exception {
        SipClusterPO sipClusterPO = new SipClusterPO(
                       LogicIdUtil.bussinessId(),  //sip服务器
                       saveDTO.getSipName(),  //sip 名称
                       saveDTO.getSipAddress(), //sip 地址
                       saveDTO.getAreaId(),    //区域ID
                       saveDTO.getSipPort(),   // sip端口
                       saveDTO.getCountryId(), //国家ID
                       saveDTO.getTurnServer(), //穿透服务器地址
                       saveDTO.getRemark()    //本备注
        );
        sipClusterPO.setCreateTime(new Date());
        sipClusterPO.setUpdateTime(new Date());
        return iOperateSipClusterDao.save(SqlUtil.durableData(sipClusterPO, PlatformConstants.TABLE_SAVE));
    }

     /**
      * <desc>
      *      修改SIP服务器信息。
      * <desc/>
      *
      * @param  saveDTO
      * @return
      * @author shengen.T
      * @createDate 2017/9/30
      */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer updateSipInfo(OperateSaveSipClusterDTO saveDTO) throws Exception {
        SipClusterPO sipClusterPO = new SipClusterPO(
                saveDTO.getSipId(),  //sip服务器
                saveDTO.getSipName(),  //sip 名称
                saveDTO.getSipAddress(), //sip 地址
                saveDTO.getAreaId(),    //区域ID
                saveDTO.getSipPort(),   // sip端口
                saveDTO.getCountryId(), //国家ID
                saveDTO.getTurnServer(), //穿透服务器地址
                saveDTO.getRemark()    //本备注
        );
         sipClusterPO.setUpdateTime(new Date());
         return iOperateSipClusterDao.update(SqlUtil.durableData(sipClusterPO, PlatformConstants.TABLE_UPDATE));
    }

     /**
      * <desc>
      *      设置SIP服务器状态。
      * <desc/>
      *
      * @param  sipId  SIP服务器ID
      * @return status SIP服务器状态
      * @author shengen.T
      * @createDate 2017/9/30
      */
     /*  启用禁用暂时不需要
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer setSipStatus(String sipId,Integer status) throws Exception {
        return iOperateSipClusterDao.setStatus(sipId,status);
    }
     */

     /**
      * <desc>
      *      获取指定SIP服务器的详细信息。
      * <desc/>
      *
      * @param  sipId SIP服务器ID
      * @return  返回单个SIP数据对象
      * @author shengen.T
      * @createDate 2017/9/30
      */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateSipClusterVO getSipInfo(String sipId) throws Exception {
        return iOperateSipClusterDao.getOne(sipId);
    }

     /**
      * <desc>
      *      获取SIP分页数据。
      * <desc/>
      *
      * @param  pageDTO 请求参数
      * @return  返回分页数据集
      * @author shengen.T
      * @createDate 2017/9/30
      */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateSipClusterVO> getSipInfoPage(OperateSipClusterPageDTO pageDTO) throws Exception{

        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
        //分页类默认pageSize为20
        Page<OperateSipClusterVO> page = new Page<>();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateSipClusterDao.getPage(params),iOperateSipClusterDao.getPageCount(params));
        return page;
    }
}
